package com.netease.nie.facade;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.netease.nie.base.cache.redis.Redis;
import com.netease.nie.base.utils.JsonUtils;
import com.netease.nie.base.utils.PageUtils;
import com.netease.nie.common.CommonBusinessTools;
import com.netease.nie.constants.CachePrefixConstants;
import com.netease.nie.constants.DisplayTypeConstants;
import com.netease.nie.dto.ExchangeGoodsInfoListDto;
import com.netease.nie.service.CreditService;
import com.netease.nie.service.PrivilegeGoodsService;

@Service
public class PrivilegeGoodsListFacadeImpl extends CommonBusinessTools implements PrivilegeGoodsListFacade {

	private static final String PRIVILEGE_GOODS_LIST_PREFIX = CachePrefixConstants.GoodsPrefix.PRIVILEGE_GOODS_LIST_PREFIX;
	private static final String GOODS_STOCK_PREFIX = CachePrefixConstants.GoodsPrefix.GOODS_STOCK_PREFIX;
	
	@Autowired
	private PrivilegeGoodsService privilegeGoodsService;
	
	@Autowired
	private CreditService creditService;
	
	@Autowired
	private Redis redis;
	
	public List<ExchangeGoodsInfoListDto> listGoodsInfoDtoByUserPrivilegeLevel(Long userId, int page) {
		int level = creditService.queryUserCreditLevelByUserId(userId);
		int pageStartIndex = PageUtils.getPageStartIndex(page);
		int pageEndIndex = PageUtils.getPageEndIndex(page);
		List<ExchangeGoodsInfoListDto> exchangeGoodsInfoListDtoList = null;
		ExchangeGoodsInfoListDto exchangeGoodsInfoListDto = null;
		String key = PRIVILEGE_GOODS_LIST_PREFIX + level;
		boolean exists = redis.exists(key);
		if (!exists) {
			exchangeGoodsInfoListDtoList = privilegeGoodsService.queryGoodsInfoListByPrivilegeLevel(level);
			if (exchangeGoodsInfoListDtoList != null && !exchangeGoodsInfoListDtoList.isEmpty()) {
				int goodsInfoListDtoListSize = exchangeGoodsInfoListDtoList.size();
				String[] exchangeGoodsInfoListDtoArr = {};
				for (int i = 0; i < goodsInfoListDtoListSize; i++) {
					exchangeGoodsInfoListDto = exchangeGoodsInfoListDtoList.get(i);
					exchangeGoodsInfoListDtoArr = (String[]) ArrayUtils.add(exchangeGoodsInfoListDtoArr, JsonUtils.obj2String(exchangeGoodsInfoListDto));
				}
				
				if (exchangeGoodsInfoListDtoArr.length > 0) {
					redis.rpush(key, exchangeGoodsInfoListDtoArr);
				}
				exchangeGoodsInfoListDtoList = PageUtils.getPageListByIndex(exchangeGoodsInfoListDtoList, pageStartIndex, pageEndIndex);
			}
		} else {
			List<String> strList = redis.lrange(key, pageStartIndex, --pageEndIndex);
			if (strList == null || strList.isEmpty())
				return Collections.emptyList();
			else {
				exchangeGoodsInfoListDtoList = new ArrayList<>();
				int strListSize = strList.size();
				for (int i = 0; i < strListSize; i++) {
					exchangeGoodsInfoListDto = JsonUtils.getClazz(strList.get(i), ExchangeGoodsInfoListDto.class);
					computeRemainDays(exchangeGoodsInfoListDto);
					exchangeGoodsInfoListDtoList.add(exchangeGoodsInfoListDto);
				}
			}
			
		}
		
		//获取实时库存
		int exchangeGoodsInfoListDtoListSize = exchangeGoodsInfoListDtoList.size();
		String[] exchangeGoodsStockKeys = {};
		String exchangeGoodsStockKey = null;
		for (int i = 0; i < exchangeGoodsInfoListDtoListSize; i++) {
			exchangeGoodsInfoListDto = exchangeGoodsInfoListDtoList.get(i);
			exchangeGoodsStockKey = GOODS_STOCK_PREFIX + exchangeGoodsInfoListDto.getId();
			exchangeGoodsStockKeys = (String[]) ArrayUtils.add(exchangeGoodsStockKeys, exchangeGoodsStockKey);
		}
		
		if (exchangeGoodsStockKeys.length > 0) {
			List<String> strList = redis.mget(exchangeGoodsStockKeys);
			Map<Integer, Integer> exchangeGoodsStockMap = distillGoodsStock(exchangeGoodsStockKeys, strList);
			for (int i = 0; i < exchangeGoodsInfoListDtoListSize; i++) {
				exchangeGoodsInfoListDto = exchangeGoodsInfoListDtoList.get(i);
				Integer stock = exchangeGoodsStockMap.get(exchangeGoodsInfoListDto.getId());
				if (stock != null) {
					exchangeGoodsInfoListDto.setStock(stock);
				}
			}
		}
		
		//替换商品图片
		replaceGoodsInfoListDtoThumbnailUrl(exchangeGoodsInfoListDtoList, DisplayTypeConstants.WAP.getCode());
		
		return exchangeGoodsInfoListDtoList;
	}

}
